package com.jiepos.mpos.biz.operationlog.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.dao.AppLogProcLogsDao;
import com.jiepos.mpos.common.entity.App_Log_Proc_LogsEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.StringUtils;

@Service
public class ProcLogService extends BaseService {

	@Autowired
	private AppLogProcLogsDao appLogProcLogsDao;
	
	/**
	 * 获取存储过程记录Json
	 * @param pagination
	 * @param request
	 * @param app_Log_Proc_LogsEntity
	 * @return
	 */
	public Map getProcLogListJson(Pagination pagination,HttpServletRequest request,App_Log_Proc_LogsEntity app_Log_Proc_LogsEntity){
		StringBuffer sb = new StringBuffer();
		List list = new ArrayList<>();
		sb.append(" select t1.proc_name,t1.fn_name,t1.title,t1.log_level,t1.content,t1.rec_id,")
			.append(" to_char(t1.rec_crt_ts,'yyyy-MM-dd hh24:mi:ss') rec_crt_ts,t1.rec_upd_usr_id from ")
			.append(Table.T_APP_LOG_PROC_LOGS).append(" t1 where 1=1 ");
		if(!StringUtils.isBlank(app_Log_Proc_LogsEntity.getProc_name())){
			sb.append(" and t1.proc_name like ?");
			list.add("%"+app_Log_Proc_LogsEntity.getProc_name()+"%");
		}
		if(!StringUtils.isBlank(app_Log_Proc_LogsEntity.getFn_name())){
			sb.append(" and t1.fn_name like ?");
			list.add("%"+app_Log_Proc_LogsEntity.getFn_name()+"%");
		}
		if(!StringUtils.isBlank(app_Log_Proc_LogsEntity.getTitle())){
			sb.append(" and t1.title like ?");
			list.add("%"+app_Log_Proc_LogsEntity.getTitle()+"%");
		}
		if(!StringUtils.isBlank(app_Log_Proc_LogsEntity.getLog_level())){
			sb.append(" and t1.log_level = ?");
			list.add(app_Log_Proc_LogsEntity.getLog_level());
		}
		if(!StringUtils.isBlank(app_Log_Proc_LogsEntity.getRec_upd_usr_id())){
			sb.append(" and t1.rec_upd_usr_id like ?");
			list.add("%"+app_Log_Proc_LogsEntity.getRec_upd_usr_id()+"%");
		}
		if(!StringUtils.isBlank(app_Log_Proc_LogsEntity.getContent())){
			sb.append(" and t1.content like ?");
			list.add("%"+app_Log_Proc_LogsEntity.getContent()+"%");
		}
		sb.append(" order by t1.rec_crt_ts desc ");
		pagination.queryString = sb.toString();
		return appLogProcLogsDao.getPaginationMap(pagination, "t1", list);
	}
}
